| Conditions | 11 |
| Total Lines | 63 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like rewrite.ts ➔ rewrite often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import {createReadStream} from 'fs' |
||
| 15 | |||
| 16 | async function rewrite(filename: string, lines: string[], eol: string, checkMode: boolean) { |
||
| 17 | const {length} = lines |
||
| 18 | , fileExists = await $exists(filename) |
||
| 19 | |||
| 20 | let line: string = "undefined" |
||
| 21 | , row = 0 |
||
| 22 | |||
| 23 | if (fileExists) { |
||
| 24 | const lineReader = createInterface({ |
||
| 25 | input: createReadStream(filename), |
||
| 26 | crlfDelay: Infinity, |
||
| 27 | historySize: 0 |
||
| 28 | }) |
||
| 29 | |||
| 30 | let isSame = true |
||
| 31 | |||
| 32 | for await (line of lineReader) { |
||
| 33 | if (line !== lines[row]) { |
||
| 34 | isSame = false |
||
| 35 | break |
||
| 36 | } |
||
| 37 | row++ |
||
| 38 | } |
||
| 39 | |||
| 40 | lineReader.close() |
||
| 41 | |||
| 42 | if (isSame) { |
||
| 43 | if (lines[row] === "") |
||
| 44 | row++ |
||
| 45 | if (length === row) |
||
| 46 | return |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | if (checkMode) |
||
| 51 | throw new Error([ |
||
| 52 | `Content of "${filename}:${row + 1}" should be another`, |
||
| 53 | "- Expected:", |
||
| 54 | lines[row], |
||
| 55 | "+ Received:", |
||
| 56 | line |
||
| 57 | ].join("\n")) |
||
| 58 | |||
| 59 | const tempFile = await tempFileName() |
||
| 60 | , fd = await $open(tempFile, "w") |
||
| 61 | |||
| 62 | for (let i = 0; i < length; i++) |
||
| 63 | await $write(fd, `${ |
||
| 64 | i ? eol : '' |
||
| 65 | }${ |
||
| 66 | lines[i] |
||
| 67 | }`) |
||
| 68 | |||
| 69 | await $close(fd) |
||
| 70 | |||
| 71 | try { |
||
| 72 | await $rename(tempFile, filename) |
||
| 73 | } catch (error) { |
||
| 74 | /* istanbul ignore next https://github.com/askirmas/postcss-d-ts/pull/30 */ |
||
| 75 | await $copy(tempFile, filename) |
||
| 76 | /* istanbul ignore next https://github.com/askirmas/postcss-d-ts/pull/30 */ |
||
| 77 | await $unlink(tempFile) |
||
| 78 | } |
||
| 80 |